package com.offcn.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.offcn.dao.CourseDao;
import com.offcn.dao.CourseDaoImpl;
import com.offcn.entity.Course;
import com.offcn.entity.CourseUser;
import com.offcn.service.CourseService;
import com.offcn.service.CourseServiceImpl;
import com.offcn.service.CourseUserService;
import com.offcn.service.CourseUserServiceImpl;
import com.offcn.utils.BaseServlet;
import com.offcn.utils.PageUtils;
import com.offcn.utils.ResultVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

@WebServlet("/courseUser")
public class CourseUserServlet extends BaseServlet {
    //实例化service对象
    CourseUserService courseUserService = new CourseUserServiceImpl();
    //创建响应数据工具类
    ResultVO resultVO = null;
    //创建转换数据类型工具类对象
    ObjectMapper objectMapper = new ObjectMapper();
    /**
     * 分页查询
     */
    public void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        //获取参数
        int currentPage = Integer.parseInt(req.getParameter("currentPage"));
        int pageSize = Integer.parseInt(req.getParameter("pageSize"));
        String search = req.getParameter("search");
        //调用service层查询
        PageUtils<CourseUser> pageUtils = courseUserService.findByIdAndPage(currentPage,pageSize,search);
        if(pageUtils != null){
            resultVO = new ResultVO(200,"查询成功",pageUtils);
        }else {
            resultVO = new ResultVO(500,"查询失败",null);
        }
        //转换为json格式
        String json = objectMapper.writeValueAsString(resultVO);
        //输出响应
        resp.getWriter().println(json);
    }
    /**
     * 批量删除
     */
    public void delAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数
        String[] id = req.getParameterValues("id");
        //调用service层删除方法
        if(courseUserService.delAll(id)){
            resultVO = new ResultVO(200,"删除成功",null);
        }else {
            resultVO = new ResultVO(500,"删除失败",null);
        }
        //转换为json格式
        String json = objectMapper.writeValueAsString(resultVO);
        //响应数据
        resp.getWriter().println(json);
    }

    /**
     * 修改选课
     */
    public void updateCourseById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //获取参数
        int id = Integer.parseInt(req.getParameter("id"));
        int cid = Integer.parseInt(req.getParameter("cid"));
        //调用service层方法
        if(courseUserService.updateCourseById(id,cid)){
            resultVO = new ResultVO(200,"修改成功",null);
        }else {
            resultVO = new ResultVO(500,"修改失败",null);
        }
        //转换为json格式
        String json = objectMapper.writeValueAsString(resultVO);
        //响应数据
        resp.getWriter().println(json);
    }
}
